export class UnauthorizedError extends Error {
  constructor(message, data = {}) {
    super(message);
    this.name = "UnauthorizedError";
    this.statusCode = 401;
    this.data = data;
  }
}

export class HttpExceptionError extends Error {
  constructor(message, data = {}) {
    super(message);
    this.name = "HttpExceptionError";
    this.statusCode = 405;
    this.data = data;
  }
}

export class ForbiddenError extends Error {
  constructor(message, data = {}) {
    super(message);
    this.name = "ForbiddenError";
    this.statusCode = 403;
    this.data = data;
  }
}
export class BadRequestError extends Error {
  constructor(message, data = {}) {
    super(message);
    this.name = "BadRequestError";
    this.statusCode = 400;
    this.data = data;
  }
}

/**
 * 自定义错误基类
 */
class CustomError extends Error {
  constructor(message, data = {}) {
    super(message);
    this.name = this.constructor.name;
    this.data = data;
    Error.captureStackTrace(this, this.constructor);
  }
}

/**
 * 
 */
export class ConflictError extends CustomError {
  constructor(message, data = {}) {
    super(message, data);
    this.name = "ConflictError";
    this.statusCode = 409;
  }
}
/**
 * 404 资源未找到错误
 */
export class NotFoundError extends CustomError {
  constructor(resource, id, data = {}) {
    const message = resource
      ? `${resource}${id ? ` (ID: ${id})` : ""} 未找到`
      : "资源未找到";
    super(message, data);
    this.statusCode = 404;
    this.code = "RESOURCE_NOT_FOUND";
  }
}

/**
 * 统一错误响应格式
 */
export function errorResponse(res, error) {
  const statusCode = error.statusCode || 500;
  const response = {
    success: false,
    code: error.code || 'INTERNAL_ERROR',
    message: error.message || 'Internal Server Error',
    ...(error.data && { data: error.data })
  };
  
  // 开发环境下包含堆栈信息
  if (process.env.NODE_ENV === 'development') {
    response.stack = error.stack;
  }
  
  return res.status(statusCode).json(response);
}
/*
 * 错误工厂函数
*/
